var arrayOfProjectiles = [];

function CreateProjectile(x, y, stage) {
    var bullet = new Projectile(x, y, stage);
    stage.addChild(bullet.sprite);
    arrayOfProjectiles.push(bullet);
    return bullet;
}

function Projectile(x,y,stage)
{
    this.sprite = new PIXI.Sprite(getTexture("bullet"));
    this.position = new PIXI.Point(x, y);
    this.sprite.position= this.position;
	
	this.width = 32;
	this.height = 32;
	
    //return m_Projectile;
	
	this.directionX = 1;
	this.directionY = 1;
	this.damage = 2;
	this.owner = "BADGUY";
	this.range = 300;
	this.alive = true;
	
	

}

Projectile.prototype.hitTarget = function ()
	{
		this.alive = false;
		if (this.sprite != null) {
		stage.removeChild(this.sprite);
		this.sprite = null;
		}
		
	}
Projectile.prototype.update = function () {
	if (this.sprite !=null) {
		this.position.x += this.directionX;
		this.sprite.position.x += this.directionX;

		this.position.y += this.directionY;
		this.sprite.position.y += this.directionY;
		this.range--;
		if (this.range <= 0 && this.alive == true) {
			this.alive = false;
			
			this.sprite.stage.removeChild(this.sprite);
			this.sprite = null;
			
		}
	}
}

//.prototype. == public
/*
Projectile.prototype.update = function() {
    this.position.x++;
    this.sprite.position.x++;	
}
*/
function updateProjectiles() {
    for (var i = 0; i < arrayOfProjectiles.length; i++) {
        arrayOfProjectiles[i].update();
    }
}

function checkColissions()
{
	for (var i = 0; i < arrayOfProjectiles.length; i++) {
		if ( arrayOfProjectiles[i].alive) 
		{
			/*
				TO DO 
				Add logic here to get bounding box of the projectile!
				var myX = arrayOfProjectiles[i].sprite.position.x;
				var myY = arrayOfProjectiles[i].sprite.position.y;
				var myW = arrayOfProjectiles[i].width;
				var myH = arrayOfProjectiles[i].height;
			*/
			var myX = arrayOfProjectiles[i].sprite.position.x + 14;
			var myY = arrayOfProjectiles[i].sprite.position.y + 14;
			var myW = arrayOfProjectiles[i].width = 4;
			var myH = arrayOfProjectiles[i].height = 4;
			var myDmg =  arrayOfProjectiles[i].damage;
			
			if  (arrayOfProjectiles[i].owner == "BADGUY")
			{
				for (var j = 0; j < arrayOfBunnies.length; j++)
				{
					if (arrayOfBunnies[j].alive)
					{
						var bX = arrayOfBunnies[j].position.x;
						var bY = arrayOfBunnies[j].position.y;
						var bW = arrayOfBunnies[j].width;
						var bH = arrayOfBunnies[j].height;
						
						if (DOTHEYHIT(myX,myY,myW,myH,bX,bY,bW,bH))
						{
							/*
							stage.removeChild(arrayOfBunnies[j]);
							arrayOfBunnies[j].alive = false;
							*/
							arrayOfBunnies[j].hitBunny(myDmg);
							arrayOfProjectiles[i].hitTarget();
						}
					}
				}
			}
			else if (arrayOfProjectiles[i].owner == "GOODGUY")
			{
				for (var k = 0; k < arrayOfEnemies.length; k++)
				{
					if (arrayOfEnemies[k].alive)
					{
						var eX = arrayOfEnemies[k].position.x;
						var eY = arrayOfEnemies[k].position.y;
						var eW = arrayOfEnemies[k].width;
						var eH = arrayOfEnemies[k].height;
						
						if (DOTHEYHITBOMBS(myX,myY,myW,myH,eX,eY,eW,eH))
						{
							/*
							stage.removeChild(arrayOfBunnies[j]);
							arrayOfBunnies[j].alive = false;
							*/
							arrayOfEnemies[k].hitEnemy(myDmg);
							arrayOfBombs[i].hitTarget();
						}
					}
				
				}
			}
		
		}
    }
}

function DOTHEYHIT(x1,y1,w1,h1,x2,y2,w2,h2)
{
	//Top Left
	if( x2 <= x1+h1 && x2 >= x1 && y2 <= y1 +h1 && y2 >= y1 )
	{
		return true;
	}
	//Bottom Left
	if( x2 <= x1+h1 && x2 >= x1 && (y2 + h2) <= y1 +h1 && (y2 + h2) >= y1 )
	{
		return true;
	}
	//Top Right
	if( (x2 + w2) <= x1+h1 && (x2 + w2) >= x1 && y2 <= y1 + h1 && y2 >= y1 )
	{
		return true;
	}
	//Bottom Right 
	if( (x2 + w2) <= x1+h1 && (x2 + w2) >= x1 && (y2 + h2) <= y1 + h1 && (y2 + h2) >= y1 )
	{
		return true;
	}

	return false;
}


